/*
 * @(#)ItemLinkTag.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.web.taglibs.pager;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;

/**
 * @author Web Platform Development Team
 * @jsp.tag name="itemLink" body-content="JSP"
 * @deprecated backward portability
 */
public class ItemLinkTag extends TagSupport {
	static final long serialVersionUID = 685341306340624365L;

	static Log log = LogFactory.getLog(ItemLinkTag.class);

	String url;

	private String[] keys;

	boolean paging;

	/**
	 * @throws JspException JspException
	 * @return int
	 */
	public int doStartTag() throws JspException {
		HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
		String queryString;
		queryString = req.getQueryString();

		if (url == null) {
			url = req.getAttribute("requestURI") + "?cmd=item";
		} else {
			url = req.getContextPath() + url;
		}

		ListTag parent = (ListTag)getParent();
		
		for (String key : keys) {
			url += "&" + key.toLowerCase() + "=" + parent.item.get(key);
		}
		if (paging) {
			try {
				url += "&pageParams=" + URLEncoder.encode(queryString, ApplicationInfo.getPageEncoding());
			} catch (UnsupportedEncodingException e1) {
				log.warn(e1);
			}
		}
		try {
			// pageContext.getOut().print("<a href=\"" + url + "\">");
			pageContext.getOut().print(url);
		} catch (IOException e) {
			throw new JspException(e.getMessage(), e);
		}
		return EVAL_PAGE;
	}

	/*
		 * public int doEndTag() throws JspException { try {
		 * pageContext.getOut().print(" </a>"); } catch (IOException e) { throw new
		 * JspException(e.getMessage(), e); }
		 *
		 * return EVAL_PAGE; }
	 */

	/**
	 * @jsp.attribute required="true" rtexprvalue="true"
	 * @return keys
	 */
	public String getKeys() {
		return keys.toString();
	}

	public void setKeys(String keys) {
		this.keys = keys.split(",");
	}

	/**
	 * @jsp.attribute required="false" rtexprvalue="true"
	 * @return url
	 */
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @jsp.attribute required="false" rtexprvalue="true"
	 * @return paging
	 */
	public boolean getPaging() {
		return paging;
	}

	public void setPaging(boolean paging) {
		this.paging = paging;
	}
}